package com.scada.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scada.hessian.client.impl.MemberBuyServiceVisitorClient;
import com.scada.model.bean.MemberBuyService;
import com.scada.model.bean.ServiceInfo;
import com.scada.model.bean.User;
import com.scada.model.bean.vo.MemberBuyServiceVO;
import com.scada.model.dataTable.MemberBuyServiceList;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.model.search.MemberBuyServiceSearch;
import com.scada.util.DateUtil;

@Controller
@RequestMapping("buyService")
public class BuyServiceController extends BaseController{

	@Autowired
	private MemberBuyServiceVisitorClient memberBuyServiceVisitorClient;
	
	@RequestMapping(value = "toMemberMain")
	public String toMemberMain(){
		return "buyService/member/list";
	}
	
	@RequestMapping(value = "getBuyServiceMemberPageData")
	@ResponseBody
	public String getBuyServiceMemberPageData(@ModelAttribute MemberBuyServiceSearch memberBuyServiceSearch) {
		
		PageInfo<MemberBuyServiceList> pageInfo = new PageInfo<MemberBuyServiceList>();
        pageInfo.setStart(memberBuyServiceSearch.getStart());
        pageInfo.setPageSize(memberBuyServiceSearch.getLength());
        pageInfo.setDraw(memberBuyServiceSearch.getDraw());
        memberBuyServiceSearch.setServiceType(ServiceInfo.ServiceTypeCode.MEMBER.getKey());
        ResponseBean rb = memberBuyServiceVisitorClient.getMemberBuyServicePageData(memberBuyServiceSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<MemberBuyServiceList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<MemberBuyServiceList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "toAddBuyServiceMember")
	public ModelAndView toAddBuyServiceMember(ModelMap model){
		
		return new ModelAndView("buyService/member/add");
	}
	
	@RequestMapping(value = "insertMemberBuyService")
	@ResponseBody
	public String insertMemberBuyService(@ModelAttribute MemberBuyService memberBuyService){
		
		ResponseBean rb = new ResponseBean();
		Boolean re = true;
		rb = memberBuyServiceVisitorClient.validateBuyRepeat(null, DateUtil.format(memberBuyService.getBuyStartDate(), "yyyy-MM-dd"), DateUtil.format(memberBuyService.getBuyEndDate(), "yyyy-MM-dd"), memberBuyService.getMemberId(), memberBuyService.getServiceId());
		if(StringUtils.equals("2", rb.getType())){
			Integer count = (Integer)rb.getObject("result");
			if(count>=1){
				re = false;
				rb.failed("当前会员已购买过此服务，无法继续购买！");
			}
		}else{
			re = false;
			rb.failed("验证会员是否已购买服务发生错误，无法继续购买！");
		}
		
		if(re){
			try {
				User user = getCurrentUser();
				memberBuyService.setCreateId(user.getId());
				memberBuyService.setLastUpdateId(user.getId());
				memberBuyServiceVisitorClient.insertMemberBuyService(memberBuyService);
				rb.success();
			}catch (Exception e) {
				rb.failed();
			}
			
		}
		return toJson(rb);
	}
	
	@RequestMapping(value = "toEditMemberBuyService")
	public ModelAndView toEditMemberBuyService(Long id, ModelMap model){
		
		MemberBuyServiceVO mbs = null;
		try {
			ResponseBean rb = memberBuyServiceVisitorClient.getMemberBuyServiceById(id);
			if(StringUtils.equals("2", rb.getType())){
				mbs = (MemberBuyServiceVO)rb.getObject("result");
			}
		}catch (Exception e) {
			
		}
		
		model.put("mbs", mbs);
		if(mbs != null) {
			if(StringUtils.equals(ServiceInfo.ServiceTypeCode.MEMBER.getKey(), mbs.getServiceType())) {
				return new ModelAndView("buyService/member/edit");
			}else if(StringUtils.equals(ServiceInfo.ServiceTypeCode.PROJECT.getKey(), mbs.getServiceType())) {
				return new ModelAndView("buyService/project/edit");
			}
		}
		
		return null;
		
	}
	
	
	@RequestMapping(value = "updateMemberBuyService")
	@ResponseBody
	public String updateMemberBuyService(@ModelAttribute MemberBuyService memberBuyService){
		
		ResponseBean rb = new ResponseBean();
		Boolean re = true;
		rb = memberBuyServiceVisitorClient.validateBuyRepeat(memberBuyService.getId(), DateUtil.format(memberBuyService.getBuyStartDate(), "yyyy-MM-dd"), DateUtil.format(memberBuyService.getBuyEndDate(), "yyyy-MM-dd"), memberBuyService.getMemberId(), memberBuyService.getServiceId());
		if(StringUtils.equals("2", rb.getType())){
			Integer count = (Integer)rb.getObject("result");
			if(count>=1){
				re = false;
				rb.failed("当前会员已购买过此服务，无法继续购买！");
			}
		}else{
			re = false;
			rb.failed("验证会员是否已购买服务发生错误，无法继续购买！");
		}
		
		if(re){
			try {
				User user = getCurrentUser();
				memberBuyService.setLastUpdateId(user.getId());
				memberBuyServiceVisitorClient.updateMemberBuyService(memberBuyService);
				rb.success();
			}catch (Exception e) {
				rb.failed();
			}
			
		}
		
		return toJson(rb);
	}
	
	@RequestMapping(value = "toProjectMain")
	public String toProjectMain(){
		return "buyService/project/list";
	}
	
	@RequestMapping(value = "getBuyServiceProjectPageData")
	@ResponseBody
	public String getBuyServiceProjectPageData(@ModelAttribute MemberBuyServiceSearch memberBuyServiceSearch) {
		
		PageInfo<MemberBuyServiceList> pageInfo = new PageInfo<MemberBuyServiceList>();
        pageInfo.setStart(memberBuyServiceSearch.getStart());
        pageInfo.setPageSize(memberBuyServiceSearch.getLength());
        pageInfo.setDraw(memberBuyServiceSearch.getDraw());
        memberBuyServiceSearch.setServiceType(ServiceInfo.ServiceTypeCode.PROJECT.getKey());
        ResponseBean rb = memberBuyServiceVisitorClient.getMemberBuyServicePageData(memberBuyServiceSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<MemberBuyServiceList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<MemberBuyServiceList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "toAddBuyServiceProject")
	public ModelAndView toAddBuyServiceProject(ModelMap model){
		
		return new ModelAndView("buyService/project/add");
	}
	
	@RequestMapping(value = "deleteMemberBuyServiceById")
	@ResponseBody
	public String deleteMemberBuyServiceById(String ids){
		
		ResponseBean rb = new ResponseBean();
		try{
			memberBuyServiceVisitorClient.deleteMemberBuyServiceById(MemberBuyService.IsShowCode.NO.getKey(), ids);
			rb.success();
		}catch(Exception e){
			rb.failed();
		}
		return toJson(rb);
	}
	
	@RequestMapping(value = "updateIfRenew")
	@ResponseBody
	public String updateIfRenew(String type, Long id) {
		
		ResponseBean rb = null;
		if(StringUtils.equals("0", type)) {
			rb = memberBuyServiceVisitorClient.updateIfRenew(MemberBuyService.ifRenewCode.NO.getKey(), id); 
		}else {
			rb = memberBuyServiceVisitorClient.updateIfRenew(MemberBuyService.ifRenewCode.YES.getKey(), id); 
		}
		return toJson(rb);
	}
	
}
